package com.example.administrator.newfragmentation.fragment.simple;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.newfragmentation.R;
import com.example.administrator.newfragmentation.fragmentMain.BaseFragment;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ModifyDetailFragment extends BaseFragment {

    private static final String ARG_TITLE = "arg_title";
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.et_modify_title)
    EditText etModifyTitle;
    @Bind(R.id.btn_modify)
    Button btnModify;
    @Bind(R.id.btn_next)
    Button btnNext;
    private String mTitle;

    public static ModifyDetailFragment newInstance(String mTitle) {
        ModifyDetailFragment fragment = new ModifyDetailFragment();
        Bundle args = new Bundle();
        args.putString(ARG_TITLE, mTitle);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mTitle = getArguments().getString(ARG_TITLE);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_modify_detail, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        toolbar.setTitle("测试startForResult");
        etModifyTitle.setText(mTitle);

        btnModify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString(DetailFragment.KEY_RESULT_TITLE, etModifyTitle.getText().toString());
                setFragmentResult(RESULT_OK, bundle);

                Toast.makeText(_mActivity, "修改成功!", Toast.LENGTH_SHORT).show();

                //可加可不加
                hideSoftInput();
                pop();
            }
        });
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                hideSoftInput();
                start(CycleFragment.newInstance(1));
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
